package xyz.lne.researchgroupmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.neo4j.ogm.model.Result;
import org.springframework.stereotype.Service;
import xyz.lne.researchgroupmanage.entity.*;
import xyz.lne.researchgroupmanage.entity.neo4j.*;
import xyz.lne.researchgroupmanage.mapper.*;
import xyz.lne.researchgroupmanage.mapper.neo4j.*;
import xyz.lne.researchgroupmanage.service.Neo4jService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Designed By Lne.
 * 2021/3/3 10:18
 */
@Service
@RequiredArgsConstructor
public class Neo4jServiceImpl implements Neo4jService {

    private final KeywordMapper keywordMapper;
    private final KeywordResearchMapper keywordResearchMapper;
    private final KeywordResearchgroupMapper keywordResearchgroupMapper;
    private final ResearchMapper researchMapper;
    private final ResearchgroupMapper researchgroupMapper;

    private final KeywordNodeRepository keywordNodeRepository;
    private final KRGRelationShipRepository krgRelationShipRepository;
    private final KRRelationShipRepository krRelationShipRepository;
    private final ResearchGroupNodeRepository researchGroupNodeRepository;
    private final ResearchNodeRepository researchNodeRepository;

    /*
        查出所有课题组、课题、关键字新建节点
        查出课题关键词 课题组关键词新建边
     */
    @Override
    public void mySqlToNeo4j() {
//        删除neo4j所有的数据
        keywordNodeRepository.deleteAllNodeAndRelationShip();
//        关键词节点
        QueryWrapper<Keyword> keywordQueryWrapper = new QueryWrapper<>();
        keywordQueryWrapper.ne("keyword_id", 0);
        List<KeywordNode> keywordNodeList = keywordMapper.selectList(keywordQueryWrapper)
                .stream().map(KeywordNode::convertFromKeyword).collect(Collectors.toList());
        keywordNodeRepository.saveAll(keywordNodeList);
//        课题节点
        QueryWrapper<Research> researchQueryWrapper = new QueryWrapper<>();
        researchQueryWrapper.ne("research_id", 0);
        List<ResearchNode> researchNodeList = researchMapper.selectList(researchQueryWrapper)
                .stream().map(ResearchNode::convertFromResearch).collect(Collectors.toList());
        researchNodeRepository.saveAll(researchNodeList);
        //        关键词 课题 边
        QueryWrapper<KeywordResearch> keywordResearchQueryWrapper = new QueryWrapper<>();
        keywordResearchQueryWrapper.ne("research_id", 0);
        List<KeywordResearch> keywordResearchList = keywordResearchMapper.selectList(keywordResearchQueryWrapper);
        keywordResearchList.forEach(keywordResearch -> addKRRelationShip(keywordResearch.getKeywordId(), keywordResearch.getResearchId()));

/*
//        课题组节点
        QueryWrapper<ResearchGroup> researchGroupQueryWrapper = new QueryWrapper<>();
        researchGroupQueryWrapper.ne("researchgroup_id", 0);
        List<ResearchGroupNode> researchGroupNodeList = researchgroupMapper.selectList(researchGroupQueryWrapper)
                .stream().map(ResearchGroupNode::convertFromResearchGroup).collect(Collectors.toList());
        researchGroupNodeRepository.saveAll(researchGroupNodeList);

        QueryWrapper<KeywordResearchgroup> keywordResearchgroupQueryWrapper = new QueryWrapper<>();
        keywordResearchgroupQueryWrapper.ne("researchgroup_id", 0);
        List<KeywordResearchgroup> keywordResearchgroupList = keywordResearchgroupMapper.selectList(keywordResearchgroupQueryWrapper);
        keywordResearchgroupList.forEach(keywordResearchgroup -> addKRGRelationShip(keywordResearchgroup.getKeywordId(), keywordResearchgroup.getResearchgroupId()));
*/
    }

    @Override
    public Result getRelatedResearches(Integer researchId, Integer size) {
        return researchNodeRepository.findRelatedResearchByRId(researchId, size);
    }

    @Override
    public Result getRelatedResearchGroups(Integer researchGroupId, Integer size) {
        return researchGroupNodeRepository.findRelatedResearchGroupByRGId(researchGroupId, size);
    }

    @Override
    public void addResearchNode(Integer researchId, String researchName) {
        ResearchNode researchNode = new ResearchNode();
        researchNode.setResearchId(researchId);
        researchNode.setName(researchName);
        researchNodeRepository.save(researchNode);
    }

    @Override
    public void addResearchGroupNode(Integer researchGroupId, String researchGroupName) {
        ResearchGroupNode researchGroupNode = new ResearchGroupNode();
        researchGroupNode.setResearchgroupId(researchGroupId);
        researchGroupNode.setName(researchGroupName);
        researchGroupNodeRepository.save(researchGroupNode);
    }

    @Override
    public void addKeywordNode(Integer keywordId, String word) {
        KeywordNode keywordNode = new KeywordNode();
        keywordNode.setKeywordId(keywordId);
        keywordNode.setWord(word);
        keywordNodeRepository.save(keywordNode);
    }


    @Override
    public void addKRRelationShip(Integer keywordId, Integer researchId) {
        ResearchNode researchNode = researchNodeRepository.findResearchNodeByRId(researchId);
        KeywordNode keywordNode = keywordNodeRepository.findKeywordNodeByKId(keywordId);
        KRRelationShip krRelationShip = new KRRelationShip(researchNode, keywordNode);
        krRelationShipRepository.save(krRelationShip);
    }

    @Override
    public void addKRGRelationShip(Integer keywordId, Integer researchGroupId) {
        ResearchGroupNode researchGroupNode = researchGroupNodeRepository.findResearchGroupNodeByRGId(researchGroupId);
        KeywordNode keywordNode = keywordNodeRepository.findKeywordNodeByKId(keywordId);
        KRGRelationShip krgRelationShip = new KRGRelationShip(researchGroupNode, keywordNode);
        krgRelationShipRepository.save(krgRelationShip);
    }
}
